<?php
class ProjectStudentsController extends AppController {

	var $name = 'ProjectStudents';

	function index() {
		$this->ProjectStudent->recursive = 0;
		$this->set('projectStudents', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid project student', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('projectStudent', $this->ProjectStudent->read(null, $id));
	}
	
	
	function add() {
		if (!empty($this->data)) {
			$this->ProjectStudent->create();
			if ($this->ProjectStudent->save($this->data)) {
				$this->Session->setFlash(__('The project student has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project student could not be saved. Please, try again.', true));
			}
		}
		$projects = $this->ProjectStudent->Project->find('list');
		$users = $this->ProjectStudent->User->find('list');
		$this->set(compact('projects', 'users'));
	}
	
	/**
	*	Tên hàm		: add_project()		
	*	Chức năng		: Nhập dữ liệu khi đăng ký mới đề tài	
	*	Giá trị vào	: Thông tin $data_project của projects_controller gởi qua
	*					  $data_project['ProjectStudent']
	*					  $data_project['ProjectStudent']['project_id']	//Thông tin đề tài
	*					  $data_project['ProjectStudent']['user_id']	//Thông tin sinh viên	
	*					  $data_project['ProjectStudent']['is_leader']	//Nhóm trưởng
	*	Giá trị ra	: Trả lại kết quả đã tạo mới được hay không true/false
	*/
	function add_project($security_code=null) {
		$data_results = array();
		$data_results['creat_ok'] = false;
		$data_results['Project'] = null;
		$data_results['ProjectStudent'] = null;
		if (!empty($security_code)) {
			$get_project_by_security_code = $this->ProjectStudent->Project->find('all',array(
				'conditions'=>array(
					'Project.security_code'=>$security_code),
				'fields' => array(
					'Project.id',
					'Project.project_year_id',
					'Project.project_category_id',
					'Project.user_id',
					'Project.name',
					'Project.description',
					'Project.security_code',
					'Project.isStudent',
					'Project.active',
					'Project.isreported',
					'Project.created',
					'Project.modified')
				));
		
			$this->ProjectStudent->create();
			
			$this->data['ProjectStudent']['project_id']	= $get_project_by_security_code[0]['Project']['id'];	//Thông tin đề tài
			$this->data['ProjectStudent']['user_id']		= $get_project_by_security_code[0]['Project']['isStudent'];	//Thông tin sinh viên	
			$this->data['ProjectStudent']['is_leader']		= '1';	//Nhóm trưởng

			if ($this->ProjectStudent->save($this->data)) {

				$get_ProjectStudent = $this->ProjectStudent->find('all',array(
				'conditions'=>array(
					'ProjectStudent.project_id'	=>	$get_project_by_security_code[0]['Project']['id'],
					'ProjectStudent.user_id'	=>	$get_project_by_security_code[0]['Project']['isStudent'],			
				),
				'fields' => array(
					'ProjectStudent.id',
					'ProjectStudent.project_id',
					'ProjectStudent.user_id',
					'ProjectStudent.is_leader',
					'ProjectStudent.created',
					'ProjectStudent.modified',
				)
				));
				$data_results['Project'] = $get_project_by_security_code[0]['Project'];
				$data_results['ProjectStudent'] = $get_ProjectStudent[0]['ProjectStudent'];
				$data_results['creat_ok'] = true;
			}
			else{

				$data_results['creat_ok'] = false;
			}
		}
		//if 	$data_results['creat_ok'] = true; đã đăng ký thành công
		//		$data_results['creat_ok'] = felse; chưa đăng ký
		return $data_results; //Đã lưu được thông tin
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid project student', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->ProjectStudent->save($this->data)) {
				$this->Session->setFlash(__('The project student has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project student could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProjectStudent->read(null, $id);
		}
		$projects = $this->ProjectStudent->Project->find('list');
		$users = $this->ProjectStudent->User->find('list');
		$this->set(compact('projects', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for project student', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProjectStudent->delete($id)) {
			$this->Session->setFlash(__('Project student deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Project student was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function result_user(){
		$set_datas = array();
		$i=0;
		$n = 10;
		for($i=0;i<=$n;$i++){
			$set_datas[$i]['id'] = 'id '.$i;
			$set_datas[$i]['name'] = 'name '.$i;
		}
		var_dump($set_datas);
		die();
	}
}
?>